package com.suredy.app.porject.ctrl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suredy.app.porject.constants.WorkPlanMilestone;
import com.suredy.app.porject.model.vo.PayinPlanFilterModel;
import com.suredy.app.porject.model.vo.PayoutPlanFilterModel;
import com.suredy.app.porject.model.vo.WorkPlanFilterModel;
import com.suredy.app.porject.roles.SystemAtomRoles;
import com.suredy.app.porject.service.ContractPayinPlanSrv;
import com.suredy.app.porject.service.ContractPayoutPlanSrv;
import com.suredy.app.porject.service.ContractWorkPlanSrv;
import com.suredy.core.ctrl.BaseCtrl;
import com.suredy.security.entity.UserEntity;
import com.suredy.security.service.RoleSrv;
import com.suredy.security.service.User2RoleSrv;

/**
 * 收款计划管理
 * 
 * @author VIVID.G
 * @since 2015-12-28
 * @version v0.1
 */
@Controller
@RequestMapping("/contract-plan")
public class ContractPlanCtrl extends BaseCtrl {

	@Autowired
	protected RoleSrv roleSrv;

	@Autowired
	protected User2RoleSrv user2RoleSrv;

	@Autowired
	private ContractPayinPlanSrv contractPayinPlanSrv;

	@Autowired
	private ContractPayoutPlanSrv contractPayoutPlanSrv;

	@Autowired
	private ContractWorkPlanSrv contractWorkPlanSrv;

	@RequestMapping(value = "/all-payin-plan-list")
	public String allPayinPlanList(PayinPlanFilterModel search, Integer page, Integer pageSize, Model model) {
		String view = "app/task/all-payin-plan-list";

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		if (search == null)
			search = new PayinPlanFilterModel();

		model.addAttribute("search", search);
		model.addAttribute("header", this.getUserByRole("" + SystemAtomRoles.ZHUANZE_MONEY_IN));
		model.addAttribute("data", this.contractPayinPlanSrv.getAllPlanList(search, page, pageSize));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contractPayinPlanSrv.getAllPlanListCount(search));

		return view;
	}

	@RequestMapping(value = "/my-payin-plan-list")
	public String myPayinPlanList(PayinPlanFilterModel search, Integer page, Integer pageSize, Model model) {
		String view = "app/task/my-payin-plan-list";

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		if (search == null)
			search = new PayinPlanFilterModel();
		search.setPlanHeader(this.getUser().getId());

		model.addAttribute("search", search);
		model.addAttribute("data", this.contractPayinPlanSrv.getAllAlarmPlanListOf(search, page, pageSize));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contractPayinPlanSrv.getAllAlarmPlanListCountOf(search));

		return view;
	}

	@RequestMapping(value = "/all-payout-plan-list")
	public String allPayoutPlanList(PayoutPlanFilterModel search, Integer page, Integer pageSize, Model model) {
		String view = "app/task/all-payout-plan-list";

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		if (search == null)
			search = new PayoutPlanFilterModel();

		model.addAttribute("search", search);
		model.addAttribute("header", this.getUserByRole("" + SystemAtomRoles.ZHUANZE_MONEY_OUT, "" + SystemAtomRoles.ZHUANZE_CAIGOU, "" + SystemAtomRoles.ZHUANZE_TONGJI));
		model.addAttribute("data", this.contractPayoutPlanSrv.getAllPlanList(search, page, pageSize));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contractPayoutPlanSrv.getAllPlanListCount(search));

		return view;
	}

	@RequestMapping(value = "/my-payout-plan-list")
	public String myPayoutPlanList(PayoutPlanFilterModel search, Integer page, Integer pageSize, Model model) {
		String view = "app/task/my-payout-plan-list";

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		if (search == null)
			search = new PayoutPlanFilterModel();
		search.setPlanHeader(this.getUser().getId());

		model.addAttribute("search", search);
		model.addAttribute("data", this.contractPayoutPlanSrv.getAllAlarmPlanListOf(search, page, pageSize));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contractPayoutPlanSrv.getAllAlarmPlanListCountOf(search));

		return view;
	}

	@RequestMapping(value = "/all-work-plan-list")
	public String allWorkPlanList(WorkPlanFilterModel search, Integer page, Integer pageSize, Model model) {
		String view = "app/task/all-work-plan-list";

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		if (search == null)
			search = new WorkPlanFilterModel();

		model.addAttribute("search", search);
		model.addAttribute("milestone", WorkPlanMilestone.values());
		model.addAttribute("data", this.contractWorkPlanSrv.getAllPlanList(search, page, pageSize));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contractWorkPlanSrv.getAllPlanListCount(search));

		return view;
	}

	@RequestMapping(value = "/my-work-plan-list")
	public String myWorkPlanList(WorkPlanFilterModel search, Integer page, Integer pageSize, Model model) {
		String view = "app/task/my-work-plan-list";

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		if (search == null)
			search = new WorkPlanFilterModel();
		search.setPlanCreator(this.getUser().getId());

		model.addAttribute("search", search);
		model.addAttribute("milestone", WorkPlanMilestone.values());
		model.addAttribute("data", this.contractWorkPlanSrv.getAllPlanList(search, page, pageSize));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contractWorkPlanSrv.getAllPlanListCount(search));

		return view;
	}

	/**
	 * 通过角色获取用户
	 * 
	 * @param roleCode
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<UserEntity> getUserByRole(String... roleCode) {
		if (roleCode == null || roleCode.length <= 0)
			return null;

		//
		String ql = "SELECT T.user FROM User2RoleEntity T LEFT JOIN T.role R WHERE :LIKE";

		String LIKE = "";
		List<Object> params = new ArrayList<Object>();

		for (String code : roleCode) {
			if (StringUtils.isBlank(code))
				continue;

			if (!StringUtils.isBlank(LIKE))
				LIKE += " OR ";

			LIKE += "R.code LIKE BINARY(?)";
			params.add("%" + code + "%");
		}

		ql = ql.replace(":LIKE", LIKE);

		List<UserEntity> users = null;

		if (params.isEmpty())
			users = (List<UserEntity>) this.user2RoleSrv.readByQL(ql);
		else
			users = (List<UserEntity>) this.user2RoleSrv.readByQL(ql, params.toArray());

		return users == null || users.isEmpty() ? null : users;
	}

}
